package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.CourseCategoryDOMapper;
import com.livepan.server.dataobject.CourseCategoryDO;
import com.livepan.server.dataobject.CourseCategoryDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.CategoryModel;
import com.livepan.server.service.model.CourseCategoryModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseCategoryService {
    @Autowired
    private CourseCategoryDOMapper courseCategoryDOMapper;

    @Autowired
    private ValidatorImpl validator;

    public void list(PageModel pageModel) {
        PageHelper.startPage(pageModel.getPage(), pageModel.getSize());
        CourseCategoryDOExample courseCategoryDOExample = new CourseCategoryDOExample();
        courseCategoryDOExample.setOrderByClause("sort asc");
        List<CourseCategoryDO> courseCategoryDOList = courseCategoryDOMapper.selectByExample(courseCategoryDOExample);
        PageInfo<CourseCategoryDO> pageInfo = new PageInfo<>(courseCategoryDOList);
        pageModel.setTotal(pageInfo.getTotal());
        List<CourseCategoryModel> courseCategoryModelList = courseCategoryDOList.stream().map(
                this::convertCourseCategoryModelFromCourseCategoryDO
        ).collect(Collectors.toList());
        pageModel.setList(courseCategoryModelList);
    }

    public void save(CourseCategoryModel courseCategoryModel) throws BusinessException {
        ValidationResult validateResult = validator.validate(courseCategoryModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        CourseCategoryDO courseCategoryDO = CopyUtil.copy(courseCategoryModel, CourseCategoryDO.class);
        if (courseCategoryDOMapper.selectByPrimaryKey(courseCategoryDO.getId()) == null) {
            this.insert(courseCategoryDO);
        } else {
            this.update(courseCategoryDO);
        }
    }

    private void insert(CourseCategoryDO courseCategoryDO) {
        courseCategoryDO.setId(UUIDUtil.getShortUUID());
        courseCategoryDOMapper.insert(courseCategoryDO);
    }

    private void update(CourseCategoryDO courseCategoryDO) {
        courseCategoryDOMapper.updateByPrimaryKey(courseCategoryDO);
    }

    public void delete(String id) {
        courseCategoryDOMapper.deleteByPrimaryKey(id);
    }

    public void saveBatch(String courseId, List<CategoryModel> categoryModelList) {
        CourseCategoryDOExample courseCategoryDOExample = new CourseCategoryDOExample();
        courseCategoryDOExample.createCriteria().andCourseIdEqualTo(courseId);
        courseCategoryDOMapper.deleteByExample(courseCategoryDOExample);
        for (CategoryModel categoryModel : categoryModelList) {
            CourseCategoryDO courseCategoryDO = new CourseCategoryDO();
            courseCategoryDO.setCategoryId(categoryModel.getId());
            courseCategoryDO.setCourseId(courseId);
            insert(courseCategoryDO);
        }
    }

    public List<CourseCategoryModel> listCourseCategory(String courseId) {
        CourseCategoryDOExample courseCategoryDOExample = new CourseCategoryDOExample();
        courseCategoryDOExample.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseCategoryDO> courseCategoryDOList = courseCategoryDOMapper
                .selectByExample(courseCategoryDOExample);
        return courseCategoryDOList.stream().map(this::convertCourseCategoryModelFromCourseCategoryDO)
                .collect(Collectors.toList());
    }


    private CourseCategoryModel convertCourseCategoryModelFromCourseCategoryDO(CourseCategoryDO courseCategoryDO) {
        if (courseCategoryDO == null) {
            return null;
        }
        CourseCategoryModel courseCategoryModel = new CourseCategoryModel();
        BeanUtils.copyProperties(courseCategoryDO, courseCategoryModel);
        return courseCategoryModel;
    }
}
